package com.aigou.admin.controller;

import com.aigou.admin.entity.PageEntity;
import com.aigou.admin.entity.ResponseEntity;
import com.aigou.admin.entity.User;
import com.aigou.admin.service.UserService;
import com.aigou.admin.service.impl.UserServiceImpl;
import com.alibaba.fastjson2.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/user")
public class UserController extends BaseServlet {

    private UserService userService = new UserServiceImpl();

    /**
     * 登录验证功能
     * @param req
     * @param resp
     * @throws IOException
     */
    public void login(HttpServletRequest req, HttpServletResponse resp) throws IOException{
        String userName = req.getParameter("username");
        String userPwd = req.getParameter("password");
        User user = userService.login(userName, userPwd);
        //将登录验证结果发送前端
        ResponseEntity.ok(resp,user);
    }

    //将方法名称作为参数传递  m=save
    public void save(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String userId = req.getParameter("user_id");
        String userName = req.getParameter("user_name");
        String userPwd = req.getParameter("user_pwd");
        String userType = req.getParameter("user_type");
        User user = new User(userId,userName,"123456",Integer.valueOf(userType));
        //查询用户
        boolean rs = userService.save(user);
        ResponseEntity.ok(resp,rs);
    }

    public void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String userId = req.getParameter("user_id");
        String userName = req.getParameter("user_name");
        String userPwd = req.getParameter("user_pwd");
        String userType = req.getParameter("user_type");
        User user = new User(userId,userName,userPwd,Integer.valueOf(userType));
        //查询用户
        boolean rs = userService.update(user);
        ResponseEntity.ok(resp,rs);
    }

    public void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String userId = req.getParameter("user_id");
        //查询用户
        boolean rs = userService.remove(userId);
        ResponseEntity.ok(resp,rs);
    }

    public void findById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //获得用户id
        String userId = req.getParameter("userId");
        //查询用户
        User user = userService.findById(userId);
        //转换为JSON,通过流发送给客户端
        ResponseEntity.ok(resp,user);
    }

    public void findAll(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //查询用户
        List<User> users = userService.findAll();
        //转换为JSON,通过流发送给客户端
        ResponseEntity.ok(resp,users);
    }

    public void page(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String current = req.getParameter("current");
        String pageSize = req.getParameter("pageSize");
        //查询用户
        PageEntity<User> users = userService.findUserPage(Integer.valueOf(current),Integer.valueOf(pageSize));
        //转换为JSON,通过流发送给客户端
        ResponseEntity.ok(resp,users);
    }
}
